const Service = require('egg').Service;


class MapService extends Service {
  
  async convertToBaiduPoint(coords) {
        const that = this;
        const res = await this.ctx.curl('http://api.map.baidu.com/geoconv/v1/', { data: {
            coords: coords,
            from: that.app.config.baidu.from,
            to:that.app.config.baidu.to,
            ak:that.app.config.baidu.ak
          },
          // 明确告诉 HttpClient 以 JSON 格式处理返回的响应 body
          dataType: 'json' });
          if(res.data.status === 0){
            return res.data.result[0];
          }
          return null;
  }

  async searchQuery(queryKey,location,pageNum){
      const that = this;
      const res = await this.ctx.curl('http://api.map.baidu.com/place/v2/search', { data: {
          location: location,
          query: queryKey,
          ak:that.app.config.baidu.ak, 
          radius:that.app.config.baidu.search.radius,
          output:that.app.config.baidu.search.output,
          ret_coordtype:that.app.config.baidu.search.retCoordtype,
          scope:that.app.config.baidu.search.scope,
          page_num:pageNum
        },
      // 明确告诉 HttpClient 以 JSON 格式处理返回的响应 body
      dataType: 'json' });
      if(res.data.status === 0){
        res.data.totalPage = this.ctx.helper.getTotalPages(res.data.total);
        return res.data;
      }
      return null;
      
  }
}

module.exports = MapService;
